package com.company.quick56.web.service.base;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;

import com.company.quick56.web.entity.Pack;
import com.company.quick56.web.util.CommonUtil;


/**
 * Spring Service Implement for domain model class Pack.
 * @see com.company.quick56.web.entity.Pack
 * @author Apusic Studio
 */
@Transactional
public class PackService extends HibernateDaoSupport implements IPackService {

	public void create(Pack instance) {
		logger.debug("persisting Pack instance");
        try {
            getHibernateTemplate().save(instance);
            logger.debug("persist successful");
        } catch (RuntimeException re) {
            logger.error("persist failed", re);
            throw re;
        }
	}
	
	public void remove(Pack instance) {
		logger.debug("removing Pack instance");
        try {
            getHibernateTemplate().delete(instance);
			logger.debug("remove successful");
        } catch (RuntimeException re) {
            logger.error("remove failed", re);
            throw re;
        }
	}
	
	public void modify(Pack instance) {
		logger.debug("modifing Pack instance");
        try {
            getHibernateTemplate().update(instance);
			logger.debug("modify successful");
        } catch (RuntimeException re) {
            logger.error("modify failed", re);
            throw re;
        }
	}
	
	public Pack findById(java.lang.Long id) {
		logger.debug("finding Pack instance with id: " + id);
        try {
            Pack instance = (Pack)getHibernateTemplate()
                    .get(Pack.class, id);
            if (instance == null) {
                logger.debug("find successful, no instance found");
            } else {
                logger.debug("find successful, instance found");
            }
            return instance;
        } catch (RuntimeException re) {
            logger.error("find failed", re);
            throw re;
        }
	}
	
	public List<Pack> findAll() {
		logger.debug("finding all the Pack instances");
        try {
			List<Pack> results = (List<Pack>) getHibernateTemplate().find("FROM Pack");
			logger.debug("find all successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            logger.error("find all failed", re);
            throw re;
        }
	}
	
	public List<Pack> find(String name, String mnemocode) {
		logger.debug("finding the pack by name=[" + name + "] and mnemocode=[" + mnemocode + "]");

		final DetachedCriteria query = DetachedCriteria.forClass(Pack.class);
		if (!CommonUtil.isBlank(name)) {
			query.add(Restrictions.eq("name", name));
		}
		if (!CommonUtil.isBlank(mnemocode)) {
			query.add(Restrictions.eq("mnemocode", mnemocode));
		}
		query.addOrder(Order.asc("name"));
		return (List) getHibernateTemplate().execute(new HibernateCallback() {

			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Criteria innerC = query.getExecutableCriteria(session);
				return innerC.list();
			}

		});
	}
}